package me.dr0n3.skyAdopt.Listeners;

import me.dr0n3.skyAdopt.Utils.AnimalHelper;
import me.dr0n3.skyAdopt.Utils.ConfigurationHelper;
import me.dr0n3.skyAdopt.Utils.MessageHelper;
import me.dr0n3.skyAdopt.skyAdopt;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Player;
import org.bukkit.entity.Wolf;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityTameEvent;

/**
 * @author dr0n3
 * @date 17.04.2012
 */
public class skyEntityListener implements Listener
{

    // <editor-fold defaultstate="collapsed" desc="Variablen">
    skyAdopt plugin;
    // </editor-fold>
    
    public skyEntityListener(skyAdopt instance) 
    {
        plugin = instance;
    }
    
    @EventHandler
    public void onEntityTame(EntityTameEvent e)
    {
        Player p = (Player) e.getOwner();
        
        if(e.getEntity() instanceof Wolf)
        {
            if(AnimalHelper.hmWolfes.get(p).size() < ConfigurationHelper.getMaxNumberOfWolfes(plugin))
            {   
                AnimalHelper.hmWolfes.get(p).add((Wolf) e.getEntity());
            }
            else
            {
                MessageHelper.sendMessageToPlayer(p, "Du hast genug Wölfe!", false);
                e.getEntity().damage(200);
            }
        }
        else if(e.getEntity() instanceof Ocelot)
        {
            if(AnimalHelper.hmCats.get(p).size() > ConfigurationHelper.getMaxNumberOfCats(plugin))
            {
                AnimalHelper.hmCats.get(p).add((Ocelot) e.getEntity());
            }
            else
            {
                MessageHelper.sendMessageToPlayer(p, "Du hast genug Katzen!", false);
                e.getEntity().damage(200);
            }
        }
    }
    
    @EventHandler
    public void onEntityDeath(EntityDeathEvent e)
    {
        if(e.getEntity() instanceof Wolf && ((Wolf) e.getEntity()).isTamed())
        {
            Player p = (Player) ((Wolf) e.getEntity()).getOwner();
            
            MessageHelper.sendMessageToPlayer(p, "Dein Hund ist gestorben :(", false);
            AnimalHelper.hmWolfes.get(p).remove((Wolf) e.getEntity());
        }
        else if(e.getEntity() instanceof Ocelot && ((Ocelot) e.getEntity()).isTamed())
        {
            Player p = (Player) ((Ocelot) e.getEntity()).getOwner();
            
            MessageHelper.sendMessageToPlayer(p, "Deine Katze ist gestorben :(", false);
            AnimalHelper.hmCats.get(p).remove((Ocelot) e.getEntity());
        }
    }
}
